DropoutGrad
计算 Dropout 的反向梯度。对每个元素:
\[dx_i = dy_i \cdot mask_i \cdot scale\]
其中 dy 为上游梯度,mask 与 scale 须与前向 Dropout 一致 (通常 scale 取 1/(1-p),p 为丢弃概率)。
- 输入:
input - 上游梯度地址(dy)
scale - 缩放因子(标量)
length - 元素个数
mask - 前向掩码地址,长度
lengthcore_mask - 核掩码(仅共享存储版本)
- 输出:
output - 输出梯度地址(dx),长度
length
- 支持平台:
FT78NEMT7004
备注
FT78NE 支持 fp32
MT7004 支持 fp16、fp32
共享存储版本:
-
void hp_dropout_grad_s(float16 *input, float16 scale, int length, float16 *output, float16 *mask, int core_mask)
-
void fp_dropout_grad_s(float *input, float scale, int length, float *output, float *mask, int core_mask)
C调用示例:
1// MT7004 示例(共享存储多核,DDR 地址)
2void TestDropoutGradSMCFp32(int length, float scale, int core_mask) {
3 int core_id = get_core_id();
4 int logic_core_id = GetLogicCoreId(core_mask, core_id);
5 int core_num = GetCoreNum(core_mask);
6 float *input = (float *)0x81000000;
7 float *output = (float *)0x82000000;
8 float *mask = (float *)0x83000000;
9 int i;
10 if (logic_core_id == 0) {
11 for (i = 0; i < length; i++) {
12 input[i] = (float)(i % 100) / 10.0f - 5.0f;
13 mask[i] = (i % 2 == 0) ? 1.0f : 0.0f;
14 }
15 }
16 sys_bar(0, core_num);
17 fp_dropout_grad_s(input, scale, length, output, mask, core_mask);
18}
19
20void main() {
21 int length = 1024;
22 float scale = 0.5f;
23 int core_mask = 0b1111;
24 TestDropoutGradSMCFp32(length, scale, core_mask);
25}
私有存储版本:
-
void hp_dropout_grad_p(float16 *input, float16 scale, int length, float16 *output, float16 *mask)
-
void fp_dropout_grad_p(float *input, float scale, int length, float *output, float *mask)
C调用示例:
1// MT7004 示例(私有存储单核,AM 地址)
2void TestDropoutGradAMFp32(int length, float scale) {
3 float *input = (float *)0x10010000;
4 float *output = (float *)0x10020000;
5 float *mask = (float *)0x10030000;
6 int i;
7 for (i = 0; i < length; i++) {
8 input[i] = (float)(i % 100) / 10.0f - 5.0f;
9 mask[i] = (i % 2 == 0) ? 1.0f : 0.0f;
10 }
11 fp_dropout_grad_p(input, scale, length, output, mask);
12}
13
14void main() {
15 int length = 1024;
16 float scale = 0.5f;
17 TestDropoutGradAMFp32(length, scale);
18}